package com.zdzx.common.config;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

@EnableAsync
@Configuration
public class CloudPoolConfig {
	@Bean("cloudExecutor")
	public Executor taskExecutor() {
		ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
		// 设置核心线程数
		int availableProcessors = Runtime.getRuntime().availableProcessors();
		executor.setCorePoolSize(availableProcessors*2);
		// 设置最大线程数
		executor.setMaxPoolSize(availableProcessors*2);
		// 设置队列容量
		executor.setQueueCapacity(1000);
		// 线程池维护线程所允许的空闲时间，默认为60s
		executor.setKeepAliveSeconds(60);
		executor.setThreadNamePrefix("taskExecutor-");
		// 设置拒绝策略
		executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
		// 等待所有任务结束后再关闭线程池
		executor.setWaitForTasksToCompleteOnShutdown(true);
		// executor.setAwaitTerminationSeconds(60*35);
		return executor;
	}
}
